package com.jr.webrtc.controller;
import com.jr.webrtc.model.*;
import com.jr.webrtc.repository.*;
import com.jr.webrtc.config.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import java.util.Optional;

@Controller
public class HomeController {

    @Autowired
    private UserRepository userRepository;
    @Autowired
    private RoomRepository roomRepository;
    @Autowired
    private PasswordEncoder passwordEncoder;
//    private BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

    @GetMapping("/home")
    public String home() {
        return "home";
    }

    @GetMapping("/login")
    public String login() {
        return "redirect:/login";
    }

    @PostMapping("/createuser")
    public ResponseEntity<?> createUser(@RequestBody User user) {
        System.out.println("Received email: " + user.getEmail()); 
        String encodedPassword = passwordEncoder.encode(user.getPassword());
        user.setPassword(encodedPassword);
        userRepository.save(user);
        return ResponseEntity.ok("已成功创建新的用户了!"); // 返回创建的用户对象
    }

    @PostMapping("/createroom")
    public ResponseEntity<?> createRoom(@RequestBody Room room) {
        roomRepository.save(room);
        return ResponseEntity.ok("已成功创建新的房间了!");
    }

    @PostMapping("/clientlogin")
    public ResponseEntity<?> login(@RequestBody LoginRequest loginRequest) {
        String username = loginRequest.getUsername();
        String password = loginRequest.getPassword();

	    Optional<User> userOptional = userRepository.findByUsername(username);

        if(userOptional.isPresent()){
	       User user = userOptional.get();
            if (passwordEncoder.matches(password, user.getPassword())) {
                return ResponseEntity.ok("zsc--JR Welcom youd:" + username);
            } else {
                return ResponseEntity.status(401).body("zsc--Invalid credentials");
            }
        } else {
            return ResponseEntity.status(401).body("zsc--User not found");
        }

//        if ("JiRui2024_1".equals(username) && "jr123456".equals(password)) {
//            return ResponseEntity.ok("JR Welcome you:" + username);
//        } else {
//            return ResponseEntity.status(401).body("Invalid credentials");
//        }        
    }    
}

class LoginRequest {
    private String username;
    private String password;

    public String getUsername() {
        return this.username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return this.password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
}